home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 January: Mac OS SDK / Dev.CD Jan 99 SDK1.toast / Development Kits / AppleScript / Development Tools / Sample Code / 7Edit 3.1 / Sources / SVEditWindow.c < prev    next >
Encoding:
Text File  |  1995-11-20  |  27.6 KB  |  1,119 lines  |  [TEXT/CWIE]

  1. // SVEditWindow.c
  2. //
  3. // 7Edit 3.1d1. Original version by Jon Lansdell and Nigel Humphreys.
  4. // 3.1 updates by Greg Sutton.
  5. // ©Apple Computer Inc 1995, all rights reserved.
  6.  
  7. /*
  8.     Changes for 3.1 :
  9.     
  10.     12-Oct-95 : CW : Cleaned up DoContent and added call to DoWindowContentDrag
  11.                      Clean up CloseMyWindow and added call to RemoveDragHandlers
  12.                      Added InstallDragHandlers call to NewDocument
  13.                      Use HiliteControl on suspend/resume instead of Show/HideControl
  14.     1-Nov-95  : DS : Made Changes for GX Printing.
  15. */
  16.  
  17. #include <Scrap.h>
  18. #include <Packages.h>
  19. #include <PLStringFuncs.h>
  20. #include <GXPrinting.h>
  21. #include <PrintingMessages.h>
  22.  
  23. #include "SVEditWindow.h"
  24. #include "SVDrag.h"
  25. #include "SVAERecording.h"
  26. #include "SVEditGXPrinting.h"
  27.  
  28. #define    kControlInvisible        0
  29. #define    kControlVisible            0xFF             
  30. #define    kScrollbarWidth            16
  31. #define    kScrollbarAdjust        (kScrollbarWidth - 1)
  32. #define    kScrollTweek            2
  33. #define    kTextOffset                5 
  34. #define    kButtonScroll            10
  35.             
  36. #define    kHOffset                20   // Stagger window offsets
  37. #define    kVOffset                20
  38.             
  39. #define    kTBarHeight                20
  40. #define    kMBarHeight                20
  41.  
  42. #pragma segment Window
  43.  
  44. DPtr    DPtrFromWindowPtr(WindowPtr theWindow)
  45. {
  46.     if (theWindow)
  47.         return((DPtr)GetWRefCon(theWindow));
  48.     else
  49.         return(NULL);
  50. } // DPtrFromWindowPtr
  51.  
  52.     
  53. #pragma segment main
  54.  
  55. /* 
  56.   Scroll the TERec around to match up to the potentially updated scrollbar
  57.   values. This is really useful when the window resizes such that the
  58.   scrollbars become inactive and the TERec had been previously scrolled.
  59. */
  60. pascal void AdjustTE(DPtr theDoc)
  61.   {
  62.        short    h;
  63.          short    v;
  64.          TEHandle myText;
  65.          
  66.          myText = theDoc->theText;
  67.        h = ((*myText)->viewRect.left - (*myText)->destRect.left) -
  68.           GetCtlValue(theDoc->hScrollBar) + kTextOffset;
  69.                          
  70.          v = ((*myText)->viewRect.top - (*myText)->destRect.top) - 
  71.               GetCtlValue(theDoc->vScrollBar) + kTextOffset;
  72.                                  
  73.      if (h || v)
  74.              {
  75.                TEScroll(h, v, theDoc->theText);
  76.                DrawPageExtras(theDoc);
  77.              }
  78.                     
  79.     }  /* AdjustTE */
  80.     
  81.     
  82. /*Calculate the new control maximum value and current value, whether it is the horizontal or
  83. vertical scrollbar. The vertical max is calculated by comparing the number of lines to the
  84. vertical size of the viewRect. The horizontal max is calculated by comparing the maximum document
  85. width to the width of the viewRect. The current values are set by comparing the offset between
  86. the view and destination rects. If necessary and we canRedraw, have the control be re-drawn by
  87. calling ShowControl.*/
  88.  
  89. /*TEStyleSample-vertical max originally used line by line calculations-lineheight was a
  90. constant value so it was easy to figure out what the range should be and pin the value
  91. within range. Now we need to use max and min values in pixels rather than in nlines*/
  92.  
  93. #pragma segment main
  94.  
  95. pascal void AdjustHV(Boolean isVert, ControlHandle control, DPtr theDoc, Boolean canRedraw)
  96.   {
  97.       TEHandle    docTE;
  98.     short       value;
  99.         short           max;
  100.         short           oldValue;
  101.         short           oldMax;
  102.         Rect             sizeRect;
  103.  
  104.         GetRectOfPage( theDoc, &sizeRect );
  105.         docTE    = theDoc->theText;
  106.     
  107.         oldValue = GetCtlValue(control);
  108.     oldMax   = GetCtlMax(control);
  109.     if (isVert)
  110.       {
  111.             /* new for TEStyleSample */
  112.                  max = TEGetHeight((*docTE)->nLines, 0, docTE) -
  113.                        ((*docTE)->viewRect.bottom - (*docTE)->viewRect.top);
  114.  
  115.        }
  116.     else
  117.       max = sizeRect.right - ((*docTE)->viewRect.right - (*docTE)->viewRect.left);
  118.                 
  119.         max += kTextOffset + kTextOffset; /* Allow over scroll by kTextOffset */
  120.                 
  121.     if (max < 0)
  122.       max = 0; /* check for negative values */
  123.             
  124.     SetCtlMax(control, max);
  125.         
  126.       if (isVert)
  127.             value = (*docTE)->viewRect.top - (*docTE)->destRect.top;
  128.         else
  129.             value = (*docTE)->viewRect.left - (*docTE)->destRect.left;
  130.                     
  131.         value += kTextOffset;
  132.                 
  133.         if (value < 0)
  134.             value = 0;
  135.         else
  136.             if (value > max)
  137.                 value = max; /* pin the value to within range */
  138.                         
  139.         SetCtlValue(control, value);
  140.         if (canRedraw && ((max != oldMax) || (value != oldValue)))
  141.             ShowControl(control); /* check to see if the control can be re-drawn */
  142.             
  143.     } /* AdjustHV */
  144.  
  145. #pragma segment Main
  146.  
  147. pascal void AdjustScrollValues(DPtr theDoc, Boolean canRedraw)
  148.  
  149. /* Simply call the common adjust routine for the vertical and horizontal scrollbars. */
  150.  
  151.   {        
  152.         AdjustHV(true,  theDoc->vScrollBar, theDoc, canRedraw);
  153.         AdjustHV(false, theDoc->hScrollBar, theDoc, canRedraw);
  154.     }        /* AdjustScrollValues */
  155.  
  156.  
  157. pascal void GetTERect(WindowPtr window, Rect *teRect)
  158.  
  159. /*   return a rectangle that is inset from the portRect by the size of
  160.      the scrollbars and a little extra margin. */
  161.  
  162.   {
  163.     *teRect = window->portRect;
  164.     (*teRect).bottom -= kScrollbarAdjust; /* and for the scrollbars */
  165.         (*teRect).right  -= kScrollbarAdjust;
  166.   }         /* GetTERect */
  167.  
  168. pascal void AdjustScrollSizes(DPtr theDoc)
  169.  
  170. /* Re-calculate the position and size of the viewRect and the scrollbars.
  171.   kScrollTweek compensates for off-by-one requirements of the scrollbars
  172.   to have borders coincide with the growbox. */
  173.  
  174.  {  
  175.    Rect    teRect;
  176.      Rect    myPortRect;
  177.  
  178.    GetTERect(theDoc->theWindow, &teRect); /*start with teRect*/
  179.      myPortRect = theDoc->theWindow->portRect;
  180.      
  181.      (*(theDoc->theText))->viewRect = teRect;
  182.  
  183.    MoveControl(theDoc->vScrollBar, myPortRect.right - kScrollbarAdjust, - 1);
  184.    SizeControl(theDoc->vScrollBar, 
  185.                  kScrollbarWidth, 
  186.                  (myPortRect.bottom - myPortRect.top) - (kScrollbarAdjust - kScrollTweek));
  187.             
  188.     MoveControl(theDoc->hScrollBar, - 1, myPortRect.bottom - kScrollbarAdjust);
  189.     SizeControl(theDoc->hScrollBar,
  190.                 (myPortRect.right - myPortRect.left) - (kScrollbarAdjust - kScrollTweek),
  191.                             kScrollbarWidth);
  192. }        /* AdjustScrollSizes */
  193.  
  194. #pragma segment Window
  195.  
  196. pascal void AdjustScrollbars(DPtr theDoc, Boolean  needsResize)
  197.  
  198. /* Turn off the controls by jamming a zero into their contrlVis fields
  199.   (HideControl erases them and we don't want that). If the controls are to
  200.   be resized as well, call the procedure to do that, then call the procedure
  201.   to adjust the maximum and current values. Finally reset the controls
  202.   to be visible if not in background. */
  203.   {
  204.  
  205.         (*(theDoc->vScrollBar))->contrlVis = kControlInvisible; /* turn them off */
  206.         (*(theDoc->hScrollBar))->contrlVis = kControlInvisible;
  207.  
  208.         if (needsResize) /* move and size if needed */
  209.             AdjustScrollSizes(theDoc);
  210.  
  211.         AdjustScrollValues(theDoc, !needsResize); /* fool with max and current value */
  212.  
  213.     /* Now, restore visibility in case we never had to ShowControl during adjustment */
  214.  
  215.         if (!gInBackground)
  216.             {
  217.                 (*(theDoc->vScrollBar))->contrlVis = kControlVisible; /* turn them on */
  218.                 (*(theDoc->hScrollBar))->contrlVis = kControlVisible;
  219.             }
  220.         else
  221.             { /* make sure they stay invisible */
  222.                 if ((*(theDoc->vScrollBar))->contrlVis)
  223.                     HideControl(theDoc->vScrollBar);
  224.                 if ((*(theDoc->vScrollBar))->contrlVis)
  225.                     HideControl(theDoc->hScrollBar);
  226.             }
  227.     }        /* AdjustScrollbars */
  228.  
  229. #pragma segment Window
  230.  
  231. pascal void GetWinContentRect(WindowPtr theWindow, Rect *r)
  232.     {
  233.         *r         = theWindow->portRect;
  234.         r->right  -= kScrollbarAdjust;
  235.         r->bottom -= kScrollbarAdjust;
  236.     }  /* GetWinContentRect */
  237.             
  238. #pragma segment Window
  239.         
  240. pascal void InvalidateDocument(DPtr theDoc)
  241.     {
  242.         GrafPtr oldPort;
  243.             
  244.     GetPort(&oldPort);
  245.         SetPort(theDoc->theWindow);
  246.         InvalRect(&theDoc->theWindow->portRect);
  247.         SetPort(oldPort);
  248.     } 
  249.                         
  250. pascal void ResizeWindow(DPtr theDoc)
  251.  
  252. /* Called when the window has been resized to fix up the controls and content */
  253.  
  254.     {
  255.         AdjustScrollbars(theDoc, true);
  256.         AdjustTE(theDoc);
  257.         InvalidateDocument(theDoc);
  258.     }         /* ResizeWindow */
  259.  
  260. pascal void ResizePageSetupForDocument(DPtr theDoc)
  261.  
  262.     /* Called when the window has been resized to fix up the controls and content */
  263.  
  264.   {
  265.         Rect pageRect;
  266.         
  267.         GetRectOfPage( theDoc, &pageRect );
  268.         (*(theDoc->theText))->destRect.right = (*(theDoc->theText))->destRect.left
  269.                                                             + pageRect.right;
  270.         TECalText(theDoc->theText);
  271.         
  272.         ResizeWindow(theDoc);
  273.     }         /* ResizePageSetupForDocument */
  274.  
  275. #pragma segment Main
  276.  
  277. pascal void CommonAction(ControlHandle control, short *amount)
  278.  
  279. /* Common algorithm for setting the new value of a control. It returns the actual amount
  280. the value of the control changed. Note the pinning is done for the sake of returning
  281. the amount the control value changed. */
  282.  
  283.     {
  284.         short   value;
  285.         short   max;
  286.  
  287.         value   = GetCtlValue(control); /* get current value */
  288.         max     = GetCtlMax(control); /* and max value */
  289.         *amount = value - *amount;
  290.         if (*amount < 0)
  291.           *amount = 0;
  292.         else
  293.             if (*amount > max)
  294.                 *amount = max;
  295.         
  296.         SetCtlValue(control, *amount);
  297.         *amount = value - *amount; /* calculate true change */
  298.     }         /* CommonAction */
  299.  
  300. #pragma segment Main
  301.  
  302. pascal void VActionProc(ControlHandle control, short part)
  303.  
  304. /* Determines how much to change the value of the vertical scrollbar by and how
  305.   much to scroll the TE record. */
  306.  
  307.   {
  308.         short           amount;
  309.         WindowPtr       window;
  310.         DPtr            theDoc;
  311.         
  312.     if (part)
  313.       {
  314.         window = (*control)->contrlOwner;
  315.                 theDoc = DPtrFromWindowPtr(window);
  316.                 switch (part) {
  317.                   case inUpButton:
  318.                     case inDownButton : amount = 24;
  319.                                         break;
  320.                   case inPageUp:
  321.                     case inPageDown   : amount = (*(theDoc->theText))->viewRect.bottom - 
  322.                                                  (*(theDoc->theText))->viewRect.top;
  323.                                         break;
  324.                                                             
  325.                 }   /* case */
  326.                 
  327.                 if (part == inDownButton || part == inPageDown)
  328.           amount = -amount; /* reverse direction */
  329.             
  330.               CommonAction(control, &amount);
  331.             
  332.                 if (amount)
  333.                     {
  334.                         TEScroll(0, amount, theDoc->theText);
  335.                         DrawPageExtras(theDoc);
  336.                     }
  337.             }     /* if */
  338.     }  /* VActionProc */
  339.  
  340. #pragma segment Main
  341.  
  342. pascal void HActionProc(ControlHandle control, short part)
  343.  
  344. /* Determines how much to change the value of the horizontal scrollbar by and how
  345.   much to scroll the TE record. */
  346.  
  347.  {
  348.    short      amount;
  349.    WindowPtr  window;
  350.    DPtr       theDoc;
  351.  
  352.         if  (part)
  353.             {
  354.                 window = (*control)->contrlOwner;
  355.                 theDoc = DPtrFromWindowPtr(window);
  356.                 switch (part) {
  357.                   case  inUpButton   :
  358.                     case  inDownButton : amount = kButtonScroll; /* a few pixels */
  359.                                          break;
  360.                   case  inPageUp   :
  361.                     case  inPageDown :   amount = (*(theDoc->theText))->viewRect.right - 
  362.                                                   (*(theDoc->theText))->viewRect.left; /* a page */
  363.                                          break;
  364.                 }   /* switch */
  365.                 if (part == inDownButton || part == inPageDown)
  366.                     amount = - amount; /* reverse direction */
  367.                     
  368.                 CommonAction(control, &amount);
  369.                 if (amount)
  370.                     {
  371.                         TEScroll(amount, 0, theDoc->theText);
  372.                         DrawPageExtras(theDoc);
  373.                     }
  374.             }     /* if */
  375.     }         /* HActionProc */
  376.  
  377. /**-----------------------------------------------------------------------
  378.         Name:             ShowSelect
  379.         Purpose:        Scrolls the text selection into view.
  380.     -----------------------------------------------------------------------**/
  381.  
  382. #pragma segment Window
  383.  
  384. pascal void ShowSelect(DPtr theDoc)
  385.   {
  386.     AdjustScrollbars(theDoc, false);
  387.  
  388.         /*
  389.             Let TextEdit do the hard work of keeping the selection visible…
  390.         */
  391.  
  392.         TEAutoView(true, theDoc->theText);
  393.         TESelView(theDoc->theText);
  394.         TEAutoView(false, theDoc->theText);
  395.  
  396.         /*
  397.             Now rematch the text and the scrollbars…
  398.         */
  399.  
  400.         SetCtlValue(theDoc->hScrollBar,
  401.                                 (*(theDoc->theText))->viewRect.left - 
  402.                                 (*(theDoc->theText))->destRect.left + kTextOffset);
  403.  
  404.         SetCtlValue(theDoc->vScrollBar,
  405.                                 (*(theDoc->theText))->viewRect.top - 
  406.                                 (*(theDoc->theText))->destRect.top  + kTextOffset);
  407.  
  408.   }  /* ShowSelect */
  409.  
  410. #pragma segment Window
  411.  
  412. pascal void OffsetWindow(WindowPtr aWindow)
  413.   {
  414.      short theWidth;
  415.      short theHeight;
  416.      short theHScreen;
  417.      short theVScreen;
  418.      short xWidth;
  419.      short xHeight;
  420.      short hMax;
  421.      short vMax;
  422.      short wLeft;
  423.      short wTop;
  424.          
  425.      theWidth  = aWindow->portRect.right - aWindow->portRect.left;
  426.      theHeight = aWindow->portRect.bottom - aWindow->portRect.top + kTBarHeight;
  427.  
  428.      theHScreen = qd.screenBits.bounds.right  - qd.screenBits.bounds.left;
  429.      theVScreen = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top;
  430.  
  431.      xWidth  = theHScreen - theWidth;
  432.      xHeight = theVScreen - (theHeight + kMBarHeight);
  433.  
  434.      hMax = (xWidth / kVOffset) + 1;
  435.      vMax = (xHeight / kVOffset) + 1;
  436.  
  437.          gWCount++;
  438.  
  439.      wLeft = (gWCount % hMax) * kVOffset;
  440.      wTop  = ((gWCount % vMax) * kVOffset) + kTBarHeight + kMBarHeight;
  441.  
  442.      MoveWindow(aWindow, wLeft, wTop, false);
  443.     }
  444.  
  445. pascal void GetLocalUpdateRgn(WindowPtr window, RgnHandle localRgn)
  446.  
  447.     /* Returns the update region in local coordinates */
  448.  
  449.     {
  450.         CopyRgn(((WindowPeek)window)->updateRgn, localRgn); /* save old update region */
  451.         OffsetRgn(localRgn, 
  452.                             window->portBits.bounds.left, 
  453.                             window->portBits.bounds.top); /* convert to local coords */
  454.     }          /* GetLocalUpdateRgn */
  455.  
  456. #pragma segment Window
  457.  
  458.  
  459.  
  460. pascal void MyGrowWindow(WindowPtr w, Point p)
  461.     {
  462.         GrafPtr savePort;
  463.     long    theResult;
  464.     Rect    r;
  465.  
  466.     GetPort(&savePort);
  467.     SetPort(w);
  468.     SetRect(&r, 80, 80, qd.screenBits.bounds.right, qd.screenBits.bounds.bottom);
  469.     theResult = GrowWindow(w, p, &r);
  470.         if (theResult)
  471.             IssueSizeWindow(w, LoWord(theResult), HiWord(theResult));
  472.  
  473.     SetPort(savePort);
  474.     }
  475.  
  476. #pragma segment Window
  477.  
  478. pascal void DoZoom(WindowPtr w,
  479.                    short     c,
  480.                    Point     p)
  481.  
  482.  {
  483.    GrafPtr savePort;
  484.  
  485.      GetPort(&savePort);
  486.    SetPort(w);
  487.      if (TrackBox(w, p, c))
  488.          {
  489.              EraseRect(&w->portRect);
  490.              IssueZoomCommand(w, c);
  491.          }
  492.     }
  493.  
  494. #pragma segment Window
  495.  
  496. pascal void DoContent ( WindowPtr theWindow, EventRecord theEvent )
  497. {
  498.     short            cntlCode;
  499.     short            part;
  500.     short            value;
  501.     ControlHandle    theControl;
  502.     GrafPtr            savePort;
  503.     Boolean            extend;
  504.     DPtr            theDoc;
  505.     Point            localPt;
  506.     
  507.     
  508.     GetPort ( &savePort );
  509.     SetPort ( theWindow );
  510.     theDoc = DPtrFromWindowPtr ( theWindow );
  511.     
  512.     localPt = theEvent.where;
  513.     GlobalToLocal ( &localPt );
  514.     cntlCode = FindControl ( localPt, theWindow, &theControl );
  515.     if ( cntlCode == 0 )
  516.     {
  517.         if ( UserWantsToDrag ( theWindow, theEvent.where ) )
  518.         {
  519.             if ( DoWindowContentDrag ( theWindow, &theEvent ) )
  520.                 TEClick ( localPt, false, theDoc->theText );
  521.         }
  522.         else
  523.         {
  524.             /*only extend the selection if the shiftkey is down*/
  525.             extend = (theEvent.modifiers & shiftKey);
  526.             if ( PtInDocument ( localPt, theDoc ) )
  527.                 TEClick ( localPt, extend, theDoc->theText );
  528.         }
  529.     }
  530.     else if ( cntlCode == inThumb )
  531.     {
  532.         value = GetCtlValue(theControl);
  533.         part  = TrackControl(theControl, localPt, nil);
  534.         if (part)
  535.         {
  536.             value -= GetCtlValue(theControl);
  537.             if (value)
  538.             {
  539.                 if (theControl == theDoc->vScrollBar)
  540.                     TEScroll(0, value, theDoc->theText);
  541.                 else
  542.                     TEScroll(value, 0, theDoc->theText);
  543.                 DrawPageExtras(theDoc);
  544.             }
  545.         }
  546.     }
  547.     else
  548.     {
  549.         if (theControl == theDoc->vScrollBar)
  550.             part = TrackControl(theControl, localPt, (ControlActionUPP) gVScrollActionUPP);
  551.         else
  552.             part = TrackControl(theControl, localPt, (ControlActionUPP) gHScrollActionUPP);
  553.     }
  554.  
  555.     SetPort(savePort);
  556. }
  557.  
  558.  
  559.  
  560. void DoBackgroundContent ( WindowPtr theWindow, EventRecord theEvent)
  561. {
  562.     Point            thePoint;
  563.     GrafPtr            savePort;
  564.     RgnHandle        dragRgn;
  565.     DPtr              theDoc;
  566.  
  567.     GetPort ( &savePort );
  568.     SetPort ( theWindow );
  569.     theDoc = DPtrFromWindowPtr ( theWindow );
  570.     thePoint = theEvent.where;
  571.     GlobalToLocal ( &thePoint );
  572.     
  573.     
  574.     dragRgn = NewRgn ( );
  575.     GetSelectedTextRgn ( theDoc, dragRgn );
  576.     if ( PtInRgn ( thePoint, dragRgn ) )
  577.     {
  578.         if ( DoWindowContentDrag ( theWindow, &theEvent ) )
  579.             SelectWindow (theWindow );
  580.     }
  581.     else
  582.         SelectWindow ( theWindow );
  583.     
  584.     SetPort ( savePort );
  585.  
  586.     return;
  587. }
  588.  
  589.  
  590.  
  591. /*
  592. pascal void DoContent(WindowPtr    theWindow,
  593.                       EventRecord  theEvent)
  594.  
  595.   {
  596.       short         cntlCode;
  597.       short         part;
  598.       ControlHandle theControl;
  599.       GrafPtr       savePort;
  600.       Boolean       extend;
  601.       DPtr          theDoc;
  602.       short         value;
  603.  
  604.     GetPort(&savePort);
  605.     SetPort(theWindow);
  606.     theDoc = DPtrFromWindowPtr(theWindow);
  607.  
  608.     GlobalToLocal(&theEvent.where);
  609.     cntlCode = FindControl(theEvent.where, theWindow, &theControl);
  610.     if (cntlCode == 0)
  611.       {
  612.               //only extend the selection if the shiftkey is down
  613.         extend = (theEvent.modifiers & shiftKey);
  614.  
  615.                 if (PtInRect(theEvent.where, &(*(theDoc->theText))->viewRect))
  616.                     TEClick(theEvent.where, extend, theDoc->theText);
  617.             }
  618.     else
  619.       if (cntlCode == inThumb)
  620.         {
  621.                     value = GetCtlValue(theControl);
  622.                     part  = TrackControl(theControl, theEvent.where, nil);
  623.                     if (part)
  624.                         {
  625.                             value -= GetCtlValue(theControl);
  626.                             if (value)
  627.                                 {
  628.                                     if (theControl == theDoc->vScrollBar)
  629.                                         TEScroll(0, value, theDoc->theText);
  630.                                     else
  631.                                         TEScroll(value, 0, theDoc->theText);
  632.                                     DrawPageExtras(theDoc);
  633.                                 }
  634.                         } 
  635.                 }
  636.             else
  637.                 if (theControl == theDoc->vScrollBar)
  638.                     part = TrackControl(theControl, theEvent.where, gVScrollActionUPP);
  639.                 else
  640.                     part = TrackControl(theControl, theEvent.where, gHScrollActionUPP);
  641.  
  642.         SetPort(savePort);
  643.     }
  644. */    
  645.  
  646. #pragma segment Window
  647.  
  648.  
  649. pascal OSErr DoActivate(WindowPtr theWindow, Boolean   activate)
  650. {
  651.     OSErr err;
  652.     Rect  r;
  653.     DPtr  theDoc;
  654.     
  655.  
  656.       err = noErr;
  657.             
  658.             if (theWindow)
  659.                 if (Ours(theWindow))
  660.                     {
  661.                         theDoc = DPtrFromWindowPtr(theWindow);
  662.                         SetPort(theWindow);
  663.                         DrawGrowIcon(theWindow);
  664.                         GetWinContentRect(theWindow, &r);
  665.                         InvalRect(&r);
  666.                         if (activate)
  667.                             {
  668.                                 TEActivate(theDoc->theText);
  669.                                 HiliteControl ( theDoc->vScrollBar, 0 );
  670.                                 HiliteControl ( theDoc->hScrollBar, 0 );
  671.                                 
  672.                                 DisableItem(myMenus[editM], undoCommand);
  673.                                 err = TEFromScrap();
  674.                                 if (gWCount == 0)
  675.                                     SetShortMenus();
  676.                             }
  677.                         else
  678.                             {
  679.                                 TEDeactivate(theDoc->theText);
  680.                                 HiliteControl ( theDoc->vScrollBar, 255 );
  681.                                 HiliteControl ( theDoc->hScrollBar, 255 );
  682.                                 
  683.                                 err = ZeroScrap();
  684.                                 err = TEToScrap();
  685.                             }
  686.                     }
  687.           return(err);
  688.         }
  689.  
  690. #pragma segment Window
  691.  
  692. pascal void GetPageEnds(short          pageHeight,
  693.                                               TEHandle       theText,
  694.                                               PageEndsArray  pageBounds,
  695.                                               short          *nPages)
  696.     {
  697.       short  pageBase;      /* total pixel offset of pages so far */
  698.       short  thisLine;
  699.       short  lastLine;
  700.       short  pageSoFar;
  701.       short  thisPage;      /* Current page being calced */
  702.       short  thisLineH;     /* Height of text line */
  703.       short  pageFirstLine; /* Line # of top of page */
  704.             
  705.         pageBase   = 0;
  706.         thisLine   = 1;
  707.         lastLine   = (*theText)->nLines;
  708.         
  709.         thisPage   = 0;
  710.         pageSoFar  = 0;
  711.         while ((thisLine <= lastLine) || (pageSoFar!=0))
  712.             {
  713.                 pageFirstLine = thisLine;
  714.                 thisLineH     = TEGetHeight(thisLine, thisLine, theText);
  715.                 
  716.                 while ((thisLineH+pageSoFar<pageHeight) && (thisLine <= lastLine))
  717.                     {
  718.                         pageSoFar += thisLineH;
  719.                         thisLine++;
  720.                         thisLineH = TEGetHeight(thisLine, thisLine, theText);
  721.                     }
  722.                     
  723.                 if (pageSoFar)
  724.                     {
  725.                         pageBounds[thisPage] = pageSoFar+pageBase;
  726.                         pageBase  = pageBounds[thisPage];
  727.                         thisPage++;
  728.                         pageSoFar = 0;
  729.                     }
  730.                     
  731.                 /*
  732.                     Special case text line taller than page
  733.                 */
  734.                 
  735.                 if ((thisLine  == pageFirstLine) &&
  736.                      (thisLineH > pageHeight))
  737.                     {
  738.                         do {
  739.                             pageBounds[thisPage] = pageBase+pageHeight;
  740.                             pageBase   = pageBounds[thisPage];
  741.                             thisPage  += 1;
  742.                             thisLineH -= pageHeight;
  743.                         } while (thisLineH >= pageHeight);
  744.                         pageSoFar = thisLineH; /* Carry bottom of large line to next page */
  745.                         thisLine += 1; /* carry xs on as pageSoFar and start measuring next line */
  746.                     }
  747.             }
  748.             
  749.         *nPages = thisPage;
  750.         
  751.     }  /* GetPageEnds */
  752.         
  753. pascal void DrawPageBreaks(DPtr theDoc)
  754.     {
  755.         PageEndsArray    pageEnds;
  756.         short           nPages;
  757.         short           ctr;
  758.         short           lineBase;
  759.         short           pageHeight;
  760.         Rect            viewRect;
  761.         Rect            pageRect;
  762.                     
  763.         GetRectOfPage( theDoc, &pageRect );
  764.         pageHeight = pageRect.bottom - pageRect.top;
  765.                 
  766.         GetPageEnds(pageHeight,
  767.                                 theDoc->theText,
  768.                                 pageEnds,
  769.                                 &nPages);
  770.                                             
  771.         lineBase = (*(theDoc->theText))->destRect.top;
  772.         viewRect = (*(theDoc->theText))->viewRect;
  773.  
  774.         PenPat(&qd.gray);
  775.         for (ctr = 0; ctr<nPages-1; ctr++)
  776.             {
  777.                 MoveTo(viewRect.left, lineBase+pageEnds[ctr]);
  778.                 LineTo(viewRect.right,lineBase+pageEnds[ctr]);
  779.             }
  780.         PenNormal();
  781.     } /*    DrawPageBreaks */
  782.             
  783. pascal void DrawPageExtras(DPtr theDoc)
  784. {
  785.     GrafPtr   oldPort;
  786.     RgnHandle    oldClip;
  787.     Rect          rectToClip;
  788.             
  789.     GetPort(&oldPort);
  790.     SetPort(theDoc->theWindow);
  791.                 
  792.     oldClip = NewRgn();
  793.     GetClip(oldClip);
  794.                 
  795.     GetWinContentRect(theDoc->theWindow,&rectToClip);
  796.     ClipRect(&rectToClip);
  797.                         
  798.     /* and then the page breaks */
  799.     DrawPageBreaks(theDoc);
  800.     
  801.     SetClip(oldClip);
  802.     
  803.     DisposeRgn(oldClip);
  804.     
  805.     SetPort(oldPort);
  806. }  /* DrawPageExtras */
  807.  
  808. pascal void DoUpdate ( WindowPtr theWindow )
  809. {
  810.     GrafPtr        savePort;
  811.     Rect        rectClip;
  812.     DPtr        theDocument;
  813.     
  814.     
  815.     theDocument = DPtrFromWindowPtr ( theWindow );
  816.     if ( Ours ( theWindow ) )
  817.     {
  818.         GetPort ( &savePort );
  819.         SetPort ( theWindow );
  820.         BeginUpdate ( theWindow );
  821.         
  822.         ClipRect(&theWindow->portRect);
  823.         EraseRect(&theWindow->portRect);
  824.         DrawControls(theWindow);
  825.         DrawGrowIcon(theWindow);
  826.         
  827.         GetWinContentRect(theWindow, &rectClip);
  828.         ClipRect(&rectClip);
  829.         
  830.         TEUpdate(&theWindow->portRect, theDocument->theText);
  831.         
  832.         DrawPageExtras(theDocument);
  833.         
  834.         EndUpdate(theWindow);
  835.         ClipRect(&theWindow->portRect);
  836.         
  837.         SetPort ( savePort );
  838.     }
  839.     
  840.     return;
  841. } /* DoUpdate */
  842.  
  843.  
  844.  
  845. #pragma segment Window
  846.  
  847. DPtr    NewDocument(Boolean isForOldDoc, WindowPtr behindWindow)
  848. {
  849.     Rect           destRect;
  850.     Rect           viewRect;
  851.     Rect           vScrollRect;
  852.     Rect           hScrollRect;
  853.     DPtr           myDoc;
  854.     WindowPtr      myWindow;
  855.     ControlHandle  vScroll;
  856.     ControlHandle  hScroll;
  857.     Str255         theName;
  858.     Str255         newNumber;
  859.     Rect           pageRect;
  860.  
  861.       if (!gWCount)
  862.           SetLongMenus();
  863.  
  864.         myDoc = nil;
  865.           myWindow = GetNewWindow(WindowID, nil, behindWindow);
  866.         if (myWindow)
  867.             {
  868.                 if (isForOldDoc==false)
  869.                     {
  870.                         GetWTitle(myWindow, theName);
  871.                         NumToString(++gNewDocCount, newNumber);
  872.                         if (gNewDocCount>1)
  873.                             {
  874.                                 PLstrcat(theName, (unsigned char *)"\p #");
  875.                                 PLstrcat(theName, newNumber);
  876.                                 SetWTitle(myWindow, theName);
  877.                             }
  878.                     }
  879.                     
  880.                 OffsetWindow(myWindow);
  881.  
  882.                 SetPort(myWindow);
  883.  
  884.                 myDoc = (DPtr)NewPtr(sizeof(DocRec));
  885.  
  886.                 SetWRefCon(myWindow, (long)myDoc);
  887.  
  888.                 myDoc->theWindow = myWindow;
  889.  
  890.                 vScrollRect = myWindow->portRect;
  891.  
  892.                 vScrollRect.left  = vScrollRect.right - kScrollbarAdjust;
  893.                 vScrollRect.right = vScrollRect.left  + kScrollbarWidth;
  894.  
  895.                 vScrollRect.bottom = vScrollRect.bottom - 14;
  896.                 vScrollRect.top    = vScrollRect.top - 1;
  897.                 
  898.                 vScroll = NewControl(myWindow, &vScrollRect, (unsigned char *)"\pScrollBar", true, 0, 0, 0, scrollBarProc, 0);
  899.  
  900.                 hScrollRect = myWindow->portRect;
  901.                 hScrollRect.top = hScrollRect.bottom - kScrollbarAdjust;
  902.                 hScrollRect.bottom = hScrollRect.top + kScrollbarWidth;
  903.  
  904.                 hScrollRect.right = hScrollRect.right - 14;
  905.                 hScrollRect.left  = hScrollRect.left - 1;
  906.                 hScroll = NewControl(myWindow, &hScrollRect, (unsigned char *)"\pScrollBar", true, 0, 0, 0, scrollBarProc, 0);
  907.  
  908.                 myDoc->vScrollBar = vScroll;
  909.                 myDoc->hScrollBar = hScroll;
  910.                 myDoc->lastID = 0;
  911.                 
  912.             
  913.                 myDoc->dirty = false;
  914.  
  915.                 GetTERect(myWindow, &viewRect);
  916.                 destRect = viewRect;
  917.  
  918.                 myDoc->theFont  = times;
  919.                 myDoc->theStyle = 0;
  920.                 myDoc->theSize  = 12;
  921.  
  922.                 myDoc->documentJob   = nil;
  923.                 myDoc->thePrintSetup = nil;
  924.                 
  925.                 if (gGXIsPresent)
  926.                 {
  927.                     if (GXNewJob(&(myDoc->documentJob)) == noErr)
  928.                         GXInstallApplicationOverride(myDoc->documentJob,
  929.                                                      gxPrintingEventMsg,
  930.                                                      NewGXPrintingEventProc(GXPrintingEventOverride));
  931.                 }
  932.                 else
  933.                 {
  934.                     myDoc->thePrintSetup = (THPrint)NewHandle(sizeof(TPrint));
  935.                     
  936.                     PrOpen();
  937.                     PrintDefault(myDoc->thePrintSetup);
  938.                     PrClose();
  939.                 }
  940.             
  941.             
  942.                 GetRectOfPage( myDoc, &pageRect );
  943.                 destRect.right = destRect.left + pageRect.right;
  944.                 
  945.                 OffsetRect(&destRect, kTextOffset, kTextOffset);
  946.                 
  947.                 TextFont(times);
  948.                 TextSize(12);
  949.                 TextFace(0);
  950.  
  951.                 myDoc->theText = TEStylNew(&destRect, &viewRect);
  952.  
  953.     /*
  954.     SetClikLoop(@AutoScroll, myDoc->theText);
  955.     */
  956.  
  957.                 myDoc->theFileName[0] = 0;
  958.                 myDoc->everSaved      = false;
  959.                 myDoc->theWindow      = myWindow;
  960.                 ResizeWindow(myDoc);
  961.                 
  962.                 InstallDragHandlers ( myWindow );
  963.             }
  964.     return(myDoc);
  965.   }
  966.  
  967. #pragma segment Window
  968.  
  969. pascal void CloseMyWindow(WindowPtr aWindow)
  970. {
  971.     DPtr     aDocument;
  972.     TEHandle theText;
  973.  
  974.     HideWindow(aWindow);
  975.     aDocument = DPtrFromWindowPtr(aWindow);
  976.     
  977.     theText = aDocument->theText;
  978.     TEDispose(theText);
  979.     
  980.     if (aDocument->thePrintSetup)
  981.         DisposHandle((Handle)aDocument->thePrintSetup);
  982.     
  983.     if (aDocument->documentJob)
  984.         GXDisposeJob(aDocument->documentJob);
  985.     
  986.     RemoveDragHandlers ( aWindow );
  987.     
  988.     DisposPtr((Ptr)aDocument);
  989.     DisposeWindow(aWindow);
  990.     
  991.     gWCount--;
  992.     
  993.     /*if there are no more windows open, set up the short menus*/
  994.     if (gWCount == 0)
  995.         SetShortMenus();
  996.     
  997. }
  998.  
  999.             
  1000.     /*
  1001.         Name     : PrintWindow
  1002.         Function : Prints the document supplied in theDoc. askUser controls interaction
  1003.                    with the user.
  1004.                              
  1005.                              Uses extra memory equal to the size of the textedit use in the 
  1006.                              printed document.
  1007.     */
  1008.     
  1009. pascal void PrintWindow(DPtr theDoc, Boolean askUser)
  1010.     {
  1011.       GrafPtr         oldPort;
  1012.     TEHandle        printerTE;
  1013.     TPPrPort             printerPort;
  1014.         Rect                     printView;
  1015.          PageEndsArray     pageBounds;
  1016.          short                  nPages;
  1017.          short                   pageCtr;
  1018.          Boolean                 abort;
  1019.          Rect                     rectToClip;
  1020.         TPrStatus           thePrinterStatus;
  1021.          DialogPtr             progressDialog;
  1022.          WindowPtr       tempWind;                 // Temp window to create TERec in 
  1023.         Rect            tempWindRect = {0,0,0,0}; // Bounds for temp window
  1024.         abort = false;
  1025.             
  1026.         /*
  1027.             Preserve the current port
  1028.         */
  1029.         GetPort(&oldPort);
  1030.         PrOpen();
  1031.             
  1032.             
  1033.       if (askUser)
  1034.             abort = !PrJobDialog(theDoc->thePrintSetup);
  1035.                 
  1036.         if (abort)
  1037.             {
  1038.                     PrClose();
  1039.                     return;
  1040.             }
  1041.             
  1042.         progressDialog = GetNewDialog(1005, nil, (WindowPtr)-1);
  1043.             
  1044.         DrawDialog(progressDialog);
  1045.             
  1046.         printerPort = PrOpenDoc(theDoc->thePrintSetup, nil, nil);
  1047.         SetPort((GrafPtr)printerPort);
  1048.             
  1049. // Create a temporary window(which is not shown)
  1050.  
  1051.         tempWind = NewWindow( nil, &tempWindRect, "\p",
  1052.                                                     false, documentProc, (WindowPtr)-1,
  1053.                                                     false, 0);
  1054.  
  1055. // Duplicate the text edit rec
  1056.         printView = (*(theDoc->thePrintSetup))->prInfo.rPage;
  1057.         DuplicateStyleTERec ( theDoc->theText,
  1058.                                                     &printerTE,
  1059.                                                     &printView,
  1060.                                                     (GrafPtr)tempWind );
  1061.  
  1062.         // Work out the offsets
  1063.         
  1064.         (*printerTE)->destRect = printView; // GetPageEnds calls TECalText
  1065.             
  1066.         GetPageEnds(printView.bottom-printView.top,
  1067.                                 printerTE,
  1068.                                 pageBounds,
  1069.                                 &nPages);
  1070.             
  1071.         TEDeactivate(printerTE);
  1072.  
  1073. // Set the TERec to the printer port
  1074.         (*printerTE)->inPort = (GrafPtr)printerPort;
  1075.  
  1076.         for (pageCtr = 0; pageCtr <= nPages-1; pageCtr++)
  1077.             if (!abort)
  1078.                 {
  1079.                     PrOpenPage(printerPort, nil);
  1080.                                                 
  1081.                     rectToClip = printView;
  1082.                     
  1083.                     if (pageCtr > 0)
  1084.                         rectToClip.bottom = rectToClip.top + (pageBounds[pageCtr]-pageBounds[pageCtr-1]);
  1085.                     else
  1086.                         rectToClip.bottom = rectToClip.top + pageBounds[pageCtr];
  1087.                         
  1088.                     ClipRect(&rectToClip);
  1089.                     
  1090.                     if (PrError() == iPrAbort)
  1091.                         abort = true;
  1092.                         
  1093.                     if (! abort)
  1094.                         TEUpdate(&printView, printerTE);
  1095.                                             
  1096.                     if (PrError() == iPrAbort)
  1097.                         abort = true;
  1098.                         
  1099.                     PrClosePage(printerPort);
  1100.                     
  1101.                     TEScroll(0,rectToClip.top-rectToClip.bottom, printerTE);
  1102.                 }
  1103.  
  1104.         TEDispose(printerTE);
  1105.         DisposeWindow ( tempWind );
  1106.         PrCloseDoc(printerPort);
  1107.  
  1108.         if (( (*(theDoc->thePrintSetup))->prJob.bJDocLoop == bSpoolLoop ) && 
  1109.                 ( PrError() == noErr )  &&
  1110.                 (! abort))
  1111.             PrPicFile( theDoc->thePrintSetup, nil, nil, nil, &thePrinterStatus);
  1112.         
  1113.         PrClose();
  1114.         
  1115.         DisposDialog(progressDialog);
  1116.         
  1117.         SetPort(oldPort);
  1118.         InvalRect(&oldPort->portRect);
  1119.     }